﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Data;
using System.Data.SqlClient;

namespace Team4Mock.DAO
{
    public class UserDAO
    {
        Helper h = new Helper();
        /// <summary>
        /// check khi login
        /// </summary>
        /// <param name="account"></param>
        /// <param name="pass"></param>
        /// <returns></returns>
        public bool CheckLogin(string account, string pass)
        {
            SqlParameter[] p;
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@user", account);
            p[0].SqlDbType = SqlDbType.VarChar;
            p[1] = new SqlParameter("@pass", pass);
            p[1].SqlDbType = SqlDbType.VarChar;
            if (h.GetValueInt("CheckLogin", p) == 1)
                return true;
            else return false;
        }
        public bool CheckUser(string us)
        {
            SqlParameter[] p;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@user", us);
            p[0].SqlDbType = SqlDbType.VarChar;
            if (h.GetValueInt("Acc_CheckUser", p) == 1)
                return true;
            else
                return false;
        }
        public void ChangePass(string acc, string pass)
        {
            SqlParameter[] p;
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@acc", acc);
            p[0].SqlDbType = SqlDbType.VarChar;
            p[1] = new SqlParameter("@pass", pass);
            p[1].SqlDbType = SqlDbType.VarChar;
            h.ExecuteNonQuerry("Acc_ChangePass", p);
        }
        public void SetPassDefault(string acc,string pass)
        {
            SqlParameter[] p;
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@account", acc);
            p[0].SqlDbType = SqlDbType.VarChar;
            p[1] = new SqlParameter("@pass", pass);
            p[1].SqlDbType = SqlDbType.VarChar;
            h.ExecuteNonQuerry("Acc_SetPassDefault",p);
        }
        /// <summary>
        /// check email khi quen password
        /// </summary>
        /// <param name="account"></param>
        /// <param name="email"></param>
        /// <returns></returns>
        public bool CheckEmail(string account, string email)
        {
            SqlParameter[] p;
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@user", account);
            p[0].SqlDbType = SqlDbType.VarChar;
            p[1] = new SqlParameter("@email", email);
            p[1].SqlDbType = SqlDbType.VarChar;
            if (h.GetValueInt("CheckEmail", p) == 1)
                return true;
            else return false;
        }
        /// <summary>
        /// get role with account
        /// </summary>
        /// <param name="account"></param>
        /// <returns></returns>
        public string GetRole(string account)
        {
            SqlParameter[] p;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@user", account);
            p[0].SqlDbType = SqlDbType.VarChar;
            return h.GetValueString("GetRole",p);
        }

        /// <summary>
        /// Insert Account
        /// </summary>
        /// <param name="user"></param>
        /// <param name="pass"></param>
        /// <param name="email"></param>
        /// <param name="ques"></param>
        /// <param name="answer"></param>
        /// <param name="role"></param>
        public void Insert(string user,string pass,string email,int ques,string answer,string role)
        {
            SqlParameter[] p;
            p = new SqlParameter[6];
            p[0] = new SqlParameter("@acc", user);
            p[0].SqlDbType = SqlDbType.VarChar;

            p[1] = new SqlParameter("@pass", pass);
            p[1].SqlDbType = SqlDbType.VarChar;

            p[2] = new SqlParameter("@email", email);
            p[2].SqlDbType = SqlDbType.VarChar;

            p[3] = new SqlParameter("@role", role);
            p[3].SqlDbType = SqlDbType.VarChar;

            p[4] = new SqlParameter("@ques", ques);
            p[4].SqlDbType = SqlDbType.Int;

            p[5] = new SqlParameter("@answer", answer);
            p[5].SqlDbType = SqlDbType.NVarChar;
            h.ExecuteNonQuerry("Acc_Insert",p);
        }
        public  void Update(int id,string role)
        {
            SqlParameter[] p;
            p = new SqlParameter[2];
            p[0] = new SqlParameter("@id", id);
            p[0].SqlDbType = SqlDbType.Int;

            p[1] = new SqlParameter("@role", role);
            p[1].SqlDbType = SqlDbType.VarChar;
            h.ExecuteNonQuerry("Acc_Update",p);
        }
        /// <summary>
        /// Check password
        /// </summary>
        /// <param name="pass"></param>
        /// <returns></returns>
        public string VerifyPass(string pass)
        {
            return h.Encryption(pass);
        }
        /// <summary>
        /// Get user by id when amend
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable GetdataByID(int id)
        {
            SqlParameter[] p;
            p = new SqlParameter[1];
            p[0] = new SqlParameter("@id", id);
            p[0].SqlDbType = SqlDbType.Int;
            return h.GetData("Acc_GetUserByID", p);
        }
        /// <summary>
        /// List user
        /// </summary>
        /// <returns></returns>
        public  DataTable GetUser()
        {
            return h.GetData("Acc_GetUser");

        }
        public Image GenericImage(out string _text, int _width, int _height)
        {
            Bitmap bmp = new Bitmap(_width, _height);
            Graphics g = Graphics.FromImage(bmp);
            Random rnd = new Random();

            // Fill in the background.        
            Rectangle rect = new Rectangle(0, 0, _width, _height);
            HatchBrush hatchBrush = new HatchBrush(HatchStyle.SmallConfetti, Color.Black, Color.Gray);
            g.FillRectangle(hatchBrush, rect);

            g.DrawLine(new Pen(Color.Blue, 1), new Point(0, _height / rnd.Next(2, 4)), new Point(_width, _height / rnd.Next(2, 6)));

            // Get random text
            string text = "abcdefghijklmnopqrstuvxy";
            string s = "";
            for (int i = 0; i < 5; i++)
            {
                s += text[rnd.Next(0, text.Length - 1)];
            }
            _text = s;


            Font f = new Font("Comic Sans MS", 20, FontStyle.Regular);
            Point p = new Point(-2, -7);
            g.DrawString(s, f, Brushes.White, p);

            g.DrawLine(new Pen(Color.Red, 1), new Point(0, _height / rnd.Next(2, 4)), new Point(_width, _height / rnd.Next(2, 6)));

            return bmp;
        }
    }
}